
DEF VAR  p-adresse   AS    CHARACTER              NO-UNDO.
DEF VAR  p-ok        AS    LOGICAL INIT TRUE      NO-UNDO.
DEF VAR vcWebResp    AS CHARACTER        NO-UNDO.
DEF VAR lSucess      AS LOGICAL          NO-UNDO.
DEF VAR mResponse    AS MEMPTR           NO-UNDO.
DEF VAR mRequest       AS MEMPTR.
DEF VAR postUrl AS CHAR.
DEF VAR vcRequest as char format "x(100)" no-undo.
DEF VAR postData  as char format "x(100)" no-undo.
DEF VAR vhSocket   AS HANDLE                                NO-UNDO.
DEF VAR i AS INTEGER NO-UNDO.


CREATE SOCKET vhSocket.

vhSocket:CONNECT("-H www.saferpay.com -S 443 -ssl -nohostverify") NO-ERROR.
IF vhSocket:CONNECTED() = FALSE THEN DO:
   p-ok = FALSE.
   p-adresse = ERROR-STATUS:GET-MESSAGE(1).
   RETURN.
END.

postUrl = "/hosting/CreatePayInit.asp".
postData = "?spPassword=JFrY6io@qi"
          + "&ACCOUNTID=99867-94913159"
          + "&ORDERID=WEB-12345"
          + "&AMOUNT=100" 
          + "&CURRENCY=CHF"
          + "&LANGID=fr"
          + "&DESCRIPTION=xx"
          + "&DELIVERY=no"
          + "&NOTIFYADDRESS=castellad@gmail.com"
          + "&SUCCESSLINK=aaa"
          + "&FAILLINK=zzzzz".

.

vcRequest = 'GET ' +
            postUrl +
            postData +
            " HTTP/1.0 ~r~n" +
            "Host: secure2.nmi.com" +
            "~r~nUser-Agent: " +
            "~r~nConnection: close~r~n~r~n"
            .

SET-SIZE(mRequest)            = 0.
SET-SIZE(mRequest)            = LENGTH(vcRequest) + 1.
SET-BYTE-ORDER(mRequest)      = BIG-ENDIAN.
PUT-STRING(mRequest,1)        = vcRequest .

vhSocket:WRITE(mRequest, 1, LENGTH(vcRequest)).

PAUSE 3 NO-MESSAGE.

DO WHILE vhSocket:GET-BYTES-AVAILABLE() > 0:
          
        SET-SIZE(mResponse) = vhSocket:GET-BYTES-AVAILABLE() + 1.
        SET-BYTE-ORDER(mResponse) = BIG-ENDIAN.
        vhSocket:READ(mResponse,1,1,vhSocket:GET-BYTES-AVAILABLE()).
        vcWebResp = vcWebResp + GET-STRING(mResponse,1).
END.

i = INDEX(vcWebResp,"https://www.saferpay.com").
IF i <> 0 THEN p-adresse = SUBSTRING(vcWebResp,i).
ELSE DO:
   p-ok = FALSE.
   p-adresse = vcWebResp.
   i = INDEX(vcWebResp,"ERROR").
   IF i <> 0 THEN p-adresse = SUBSTRING(vcWebResp,i).
   ELSE p-adresse = vcWebResp.
END.

MESSAGE p-adresse.

vhSocket:DISCONNECT().
DELETE OBJECT vhSocket.
